package cn.lili.modules.system.serviceimpl;

import cn.lili.common.security.context.UserContext;
import cn.lili.modules.store.entity.dos.StoreLogistics;
import cn.lili.modules.store.entity.dto.StoreLogisticsCustomerDTO;
import cn.lili.modules.store.entity.params.StoreLogisticsParams;
import cn.lili.modules.store.entity.vos.StoreLogisticsVO;
import cn.lili.modules.system.mapper.StoreLogisticsMapper;
import cn.lili.modules.system.service.StoreLogisticsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 物流公司业务层实现
 *
 * @author Chopper
 * @since 2020/11/17 8:02 下午
 */
@Service
public class StoreLogisticsServiceImpl extends ServiceImpl<StoreLogisticsMapper, StoreLogistics> implements StoreLogisticsService {

    @Override
    public List<StoreLogisticsVO> getStoreLogistics(String storeId) {
        return this.baseMapper.getStoreLogistics(storeId);
    }

    @Override
    public List<StoreLogisticsVO> getStoreSelectedLogistics(String storeId) {
        return this.baseMapper.getSelectedStoreLogistics(storeId);

    }

    @Override
    public List<String> getStoreSelectedLogisticsName(String storeId) {
        return this.baseMapper.getSelectedStoreLogisticsName(storeId);
    }

    @Override
    public List<StoreLogisticsVO> getStoreSelectedLogisticsUseFaceSheet(String storeId) {
        return this.baseMapper.getSelectedStoreLogisticsUseFaceSheet(storeId);
    }

    @Override
    public StoreLogistics update(StoreLogisticsCustomerDTO storeLogisticsCustomerDTO) {
        LambdaQueryWrapper<StoreLogistics> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(StoreLogistics::getLogisticsId, storeLogisticsCustomerDTO.getLogisticsId());
        lambdaQueryWrapper.eq(StoreLogistics::getStoreId, storeLogisticsCustomerDTO.getExtendId());
        this.remove(lambdaQueryWrapper);
        StoreLogistics resultstoreLogistics = new StoreLogistics(storeLogisticsCustomerDTO);
        resultstoreLogistics.setStoreId(storeLogisticsCustomerDTO.getExtendId());
        resultstoreLogistics.setLogisticsId(storeLogisticsCustomerDTO.getLogisticsId());
        this.save(resultstoreLogistics);
        return resultstoreLogistics;
    }

    @Override
    public StoreLogistics getStoreLogisticsInfo(String logisticsId, String storeId) {
        return this.getOne(new LambdaQueryWrapper<StoreLogistics>().eq(StoreLogistics::getStoreId, storeId).eq(StoreLogistics::getLogisticsId, logisticsId));
    }

    @Override
    public List<StoreLogisticsVO> getOpenStoreLogistics(String storeId) {
        List<StoreLogisticsVO> openStoreLogistics = this.baseMapper.getOpenStoreLogistics(storeId);
        for (StoreLogisticsVO storeLogisticsVO : openStoreLogistics) {
            storeLogisticsVO.setSelected("1");
        }
        return openStoreLogistics;
    }

    @Override
    public List<StoreLogisticsVO> getCloseStoreLogistics(String storeId) {
        return this.baseMapper.getCloseStoreLogistics(storeId);
    }

    @Override
    public StoreLogistics add(StoreLogisticsCustomerDTO storeLogisticsCustomerDTO) {
        //判断是否已经选择过，如果没有选择则进行添加
        LambdaQueryWrapper<StoreLogistics> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(StoreLogistics::getLogisticsId, storeLogisticsCustomerDTO.getLogisticsId());
        lambdaQueryWrapper.eq(StoreLogistics::getStoreId, UserContext.getCurrentUser().getExtendId());
        StoreLogistics storeLogistics;
        if (this.getOne(lambdaQueryWrapper) == null) {
            storeLogistics = new StoreLogistics(storeLogisticsCustomerDTO);
            storeLogistics.setStoreId(UserContext.getCurrentUser().getExtendId());
            storeLogistics.setLogisticsId(storeLogisticsCustomerDTO.getLogisticsId());
            this.save(storeLogistics);
            return storeLogistics;
        }
        return null;
    }

    /**
     * 根据参数移除物流
     *
     * @param storeLogisticsParams 物流参数
     * @return 移除结果
     */
    @Override
    public boolean removeByParams(StoreLogisticsParams storeLogisticsParams) {
        LambdaQueryWrapper<StoreLogistics> lambdaQueryWrapper =
                new LambdaQueryWrapper<StoreLogistics>()
                        .eq(StoreLogistics::getId, storeLogisticsParams.getStoreLogisticsId())
                        .eq(StoreLogistics::getStoreId, storeLogisticsParams.getStoreId());
        return this.remove(lambdaQueryWrapper);
    }
}